﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpTag.ID3;
using System.IO;

namespace SharpTag.ID3.TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            AbstractFileFactory fileFactory = new FileFactory();
            AbstractTagReaderFactory tagReaderFactory = new ID3TagReaderFactory();
            if (File.Exists(args[0]))
            {
                var musicFile = fileFactory.CreateMP3File(args[0]);
                var tagReader = tagReaderFactory.CreateV1TagReader();
                var tagData = tagReader.ReadTagBytes(musicFile);
                musicFile.ParseTag(tagData);
                tagReader = tagReaderFactory.CreateV2TagReader();
                tagData = tagReader.ReadTagBytes(musicFile);
                musicFile.ParseTag(tagData);

                Console.WriteLine("========== File Data ==========");
                Console.WriteLine("File Path: '{0}'", musicFile.Path);
                Console.WriteLine("File Size: '{0}'", musicFile.Size);
                Console.WriteLine("File Type: '{0}'", musicFile.Type);

                Console.WriteLine("========== Tag Data  ==========");
                foreach (var tag in musicFile.GetTags())
                {
                    Console.WriteLine("===== Tag Version: '{0}' Data =====", tag.Version);
                    Console.WriteLine("Title: '{0}'", tag.Title);
                    Console.WriteLine("Artist: '{0}'", tag.Artist);
                    Console.WriteLine("Album: '{0}'", tag.Album);
                    Console.WriteLine("Comment: '{0}'", tag.Comment);
                    Console.WriteLine("Year: '{0}'", tag.Year);
                    Console.WriteLine("Genre: '{0}'", tag.Genre);

                    if (tag.Version >= 2.0)
                        DumpFrameData(tag);
                }

            }
            else
            {
                Console.Write("Can't find file '{0}'.  Please supply the full path to a valid file.", args[0]);
            }
            

            Console.ReadKey();
        }

        private static void DumpFrameData(ITag tag)
        {
            var v2Tag = tag as ID3V2Tag;
            Console.WriteLine("===== '{0}' frame data start =====", tag.Name);
            foreach (var frame in v2Tag.Frames)
            {
                Console.WriteLine("===== '{0}' start =====", frame.Header.Name);
                foreach (var entry in frame.Data)
                {
                    if (entry.Value is byte[])
                    {
                        var bV = entry.Value as byte[];
                        Console.Write("{0}: ", entry.Key);
                        foreach (var b in bV)
                        {
                            Console.Write(b + " ");
                        }
                        Console.WriteLine();
                        continue;
                    }
                    Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
                }
                Console.WriteLine("===== '{0}' end =====", frame.Header.Name);
            }
            Console.WriteLine("===== '{0}' frame data end =====", tag.Name);
        }
    }
}
